package leeon.forpeddy.excel;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class StudentExcelWrapper {
	private WritableWorkbook workbook;
	private WritableSheet sheet;
	
	
	public StudentExcelWrapper(File excel) throws BiffException, IOException {
		workbook = Workbook.createWorkbook(excel, Workbook.getWorkbook(excel));
		sheet = workbook.getSheet(0);		
	}

	public void save() throws IOException, WriteException {
		workbook.write();
		workbook.close();
	}
	
	public List<Map<String, String>> readAll() {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		int length = sheet.getColumn(0).length;
		for (int i = 0; i < length; i ++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("code", sheet.getCell(0, i).getContents());
			map.put("name", sheet.getCell(1, i).getContents());
			
			list.add(map);
		}
		
		return list;
	}
}
